// pages/mine/apply_record/apply_record.js
import {
  Toast
} from 'wuss-weapp';
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    result: [],
    options: ['正在投票', '已结束'],
    type: 2
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      theme: app.globalData.theme
    });
    wx.setNavigationBarTitle({
      title: '社区投票',
    })
    app.setNavigationBarColor();
    this.applyRecordLoad();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    app.globalData.pageTotalNumber = 0;
    app.globalData.pageNumber = 1;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    app.globalData.pageNumber = 1;
    this.setData({
      result: []
    });
    this.applyRecordLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
   
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  handleLoadmoreL:function(){
    app.globalData.pageNumber++;
    this.setData({
      mpLoadingShow: true,
      mpLoadingTips: "加载中..."
    });
    if (app.globalData.pageNumber <= app.globalData.pageTotalNumber) {
      this.applyRecordLoad();
    } else {
      setTimeout(() => {
        this.setData({
          mpLoadingTips: "暂无数据"
        });
      }, 500);
      setTimeout(() => {
        this.setData({
          mpLoadingShow: false
        });
      }, 1000);
    }
  },
  /**
   * 加载数据
   */
  applyRecordLoad: function() {
    app.wxGetRequest("/wechart/life/getMicroservice", {
      url: app.globalData.urlMicro + "/v1/article/page/over",
      area_code: app.globalData.parentAreaCode,
      user_id: app.globalData.userInfo["user_id"],
      "page.page_number": app.globalData.pageNumber,
      "page.page_size": app.globalData.pageSize,
      type: this.data.type
    }).then(result => {
      let pageTotalNumber = Math.ceil(result.data.count / app.globalData.pageSize);
      this.setData({
        result: this.data.result.concat(result.data.records),
        noData: pageTotalNumber == 0 ? true : false,
        mpLoadingShow: false
      });
      app.globalData.pageTotalNumber = pageTotalNumber;
    });
  },
  /**
   * 详情
   */
  voteDetail: function(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/index/vote/vote_detail/vote_detail?id=' + id,
    })
  },
  /**
   * 切换
   */
  changeType:function(e){
    let type = e.detail.value;
    app.globalData.pageNumber = 1;
    if(type==0){
      type = type+2;
    }
    this.setData({
      type:type,
      result:[]
    });
    this.applyRecordLoad();
  }
})